﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:converters="clr-namespace:MahApps.Metro.Converters">

    <!-- =============================================================================== -->
    <!-- Metro styled TreeView                                                           -->
    <!-- =============================================================================== -->

    <PathGeometry x:Key="TreeArrow"
                  Figures="M0,0 L0,6 L6,0 z" />

    <Style x:Key="ExpandCollapseToggleStyle"
           TargetType="{x:Type ToggleButton}">
        <Setter Property="Margin"
                Value="1" />
        <Setter Property="Focusable"
                Value="False" />
        <Setter Property="Width"
                Value="16" />
        <Setter Property="Height"
                Value="16" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Grid Background="Transparent"
                          Margin="{TemplateBinding Margin}"
                          Height="16"
                          Width="16">
                        <Path x:Name="ExpandPath"
                              HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Data="{StaticResource TreeArrow}"
                              Fill="Transparent"
                              Stroke="{DynamicResource BlackBrush}">
                            <Path.RenderTransform>
                                <RotateTransform Angle="135"
                                                 CenterY="3"
                                                 CenterX="3" />
                            </Path.RenderTransform>
                        </Path>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked"
                                 Value="True">
                            <Setter Property="RenderTransform"
                                    TargetName="ExpandPath">
                                <Setter.Value>
                                    <RotateTransform Angle="180"
                                                     CenterY="3"
                                                     CenterX="3" />
                                </Setter.Value>
                            </Setter>
                            <Setter Property="Fill"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource GrayBrush1}" />
                            <Setter Property="Stroke"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource BlackBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter Property="Stroke"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource GrayBrush5}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsEnabled"
                                           Value="False" />
                                <Condition Property="IsChecked"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Fill"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource GrayBrush5}" />
                            <Setter Property="Stroke"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource GrayBrush5}" />
                        </MultiTrigger>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter Property="Stroke"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource AccentColorBrush}" />
                            <Setter Property="Fill"
                                    TargetName="ExpandPath"
                                    Value="{DynamicResource AccentColorBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- obsolete -->
    <Style x:Key="TreeViewItemFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="0"
                               Opacity="0"
                               SnapsToDevicePixels="True"
                               Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                               StrokeThickness="5"
                               StrokeDashArray="1 2" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <converters:TreeViewMarginConverter Length="12"
                                        x:Key="LengthConverter" />

    <Style x:Key="MetroTreeViewItem"
           TargetType="{x:Type TreeViewItem}">
        <Setter Property="MinHeight"
                Value="25" />
        <Setter Property="HorizontalContentAlignment"
                Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        <Setter Property="VerticalContentAlignment"
                Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        <Setter Property="Foreground"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="Background"
                Value="{DynamicResource WhiteBrush}" />
        <Setter Property="FocusVisualStyle"
                Value="{x:Null}" />
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeViewItem}">
                    <StackPanel>
                        <Border x:Name="Bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                MinHeight="{TemplateBinding MinHeight}"
                                UseLayoutRounding="True"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <Grid Margin="{Binding Converter={StaticResource LengthConverter}, RelativeSource={x:Static RelativeSource.TemplatedParent}}"
                                  Background="Transparent"
                                  VerticalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition MinWidth="19"
                                                      Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <ToggleButton x:Name="Expander"
                                              Style="{StaticResource ExpandCollapseToggleStyle}"
                                              IsChecked="{Binding Path=IsExpanded, RelativeSource={x:Static RelativeSource.TemplatedParent}, Mode=TwoWay}"
                                              ClickMode="Press" />
                                <ContentPresenter x:Name="PART_Header"
                                                  Grid.Column="1"
                                                  ContentSource="Header"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Grid>
                        </Border>
                        <ItemsPresenter x:Name="ItemsHost"
                                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded"
                                 Value="False">
                            <Setter TargetName="ItemsHost"
                                    Property="Visibility"
                                    Value="Collapsed" />
                        </Trigger>

                        <Trigger Property="HasItems"
                                 Value="False">
                            <Setter TargetName="Expander"
                                    Property="Visibility"
                                    Value="Hidden" />
                        </Trigger>

                        <Trigger SourceName="Bd"
                                 Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource AccentColorBrush3}" />
                            <Setter Property="Foreground"
                                    Value="{DynamicResource BlackColorBrush}" />
                        </Trigger>
                        <Trigger Property="IsSelected"
                                 Value="True">
                            <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource AccentColorBrush}" />
                            <Setter Property="Foreground"
                                    Value="{DynamicResource AccentSelectedColorBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource GrayBrush7}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsEnabled"
                                           Value="False" />
                                <Condition Property="IsSelected"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter TargetName="Bd"
                                        Property="Background"
                                        Value="{DynamicResource GrayBrush7}" />
                                <Setter Property="Foreground"
                                        Value="{DynamicResource AccentSelectedColorBrush}" />
                            </MultiTrigger.Setters>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="True" />
                                <Condition Property="Selector.IsSelectionActive"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource AccentColorBrush2}" />
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="VirtualizingStackPanel.IsVirtualizing"
                     Value="True">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="MetroTreeView"
           TargetType="{x:Type TreeView}">
        <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource BlackBrush}" />
        <Setter Property="Background"
                Value="{DynamicResource WhiteBrush}" />
        <!-- default to 0 -->
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Padding"
                Value="1" />
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TreeView}">
                    <Border Name="Border"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                        <ScrollViewer x:Name="TreeViewScrollViewer"
                                      Focusable="False"
                                      Padding="{TemplateBinding Padding}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                      VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                      CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="VirtualizingStackPanel.IsVirtualizing"
                     Value="True">
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel />
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ScrollViewer.CanContentScroll"
                        Value="True" />
            </Trigger>

            <Trigger Property="IsEnabled"
                     Value="False">
                <Setter Property="BorderBrush"
                        Value="{DynamicResource GrayBrush9}" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="VirtualisedMetroTreeView"
           TargetType="{x:Type TreeView}"
           BasedOn="{StaticResource MetroTreeView}">
        <Setter Property="VirtualizingStackPanel.IsVirtualizing"
                Value="True" />
        <Setter Property="VirtualizingStackPanel.VirtualizationMode"
                Value="Recycling" />
    </Style>

    <Style TargetType="{x:Type TreeView}"
           BasedOn="{StaticResource MetroTreeView}" />
    <Style TargetType="{x:Type TreeViewItem}"
           BasedOn="{StaticResource MetroTreeViewItem}" />
</ResourceDictionary>